package com.zhika.zishi.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zhika.zishi.R;
import com.zhika.zishi.adapter.FragmentSelfSPAdapter;
import com.zhika.zishi.beans.ItemLetterBean;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.fragments.LetterInformFragment;
import com.zhika.zishi.interfaces.TitleInterface;
import com.zhika.zishi.util.ActivityCollector;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/19.
 * 提醒activity,包括通知和私信
 */
public class RemindActivity extends FragmentActivity {

    private ViewPager viewPager;

    private LinearLayout lineLayout;

    private LinearLayout.LayoutParams lineParams;

    private RelativeLayout letterLayout;

    private RelativeLayout informLayout;

    private TitleView titleView;

    private int screenWidth;

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    private ArrayList<ItemLetterBean> userLetterList = new ArrayList<>();

    private ArrayList<ItemLetterBean> systemLetterList = new ArrayList<>();

    private ArrayList<String> informList = new ArrayList<>();

    private FragmentSelfSPAdapter fragmentAdapter;

    private int currentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remind_layout);
        ActivityCollector.addActivity(this);

        addFragmentData();

        initView();
    }

    private void initView() {
        letterLayout = (RelativeLayout) findViewById(R.id.letter_inform_tab_letter_layout);
        letterLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });

        informLayout = (RelativeLayout) findViewById(R.id.letter_inform_tab_inform_layout);
        informLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });

        titleView = (TitleView) findViewById(R.id.remind_title_view);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("提醒");
        titleView.hideRightButton();
        titleView.setTitleViewInterface(new TitleInterface() {
            @Override
            public void leftTitleListener() {
                finish();
            }

            @Override
            public void middleTitleListener() {

            }

            @Override
            public void rightTitleListener() {

            }
        });
        viewPager = (ViewPager) findViewById(R.id.remind_view_pager);

        screenWidth = getScreenWidth();
        lineLayout = (LinearLayout) findViewById(R.id.letter_inform_tab_line_layout);
        lineParams = (LinearLayout.LayoutParams) lineLayout.getLayoutParams();
        lineLayout.post(new Runnable() {
            @Override
            public void run() {
                lineParams.width = screenWidth / 2;
                lineLayout.setLayoutParams(lineParams);
            }
        });
        fragmentAdapter = new FragmentSelfSPAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                if (currentPosition == 0&&position==0){
                    lineParams.leftMargin = (int) (currentPosition * 1.0*screenWidth/2 + positionOffset *( 1.0 *screenWidth/2));
                }else if(currentPosition==1&&position==0){
                    lineParams.leftMargin = (int) (currentPosition * 1.0 * screenWidth/2 - (1-positionOffset) *( 1.0 *screenWidth/2));
                }
                lineLayout.setLayoutParams(lineParams);
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void addFragmentData() {

        addTestData();

        LetterInformFragment letterFragment = new LetterInformFragment();
        Bundle bundle = new Bundle();
        bundle.putString("letter_inform", "letter");
        bundle.putSerializable("userLetter", userLetterList);
        bundle.putSerializable("systemLetter", systemLetterList);
        letterFragment.setArguments(bundle);
        fragmentList.add(letterFragment);

        LetterInformFragment informFragment = new LetterInformFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString("letter_inform", "inform");
        bundle1.putStringArrayList("informlist", informList);
        informFragment.setArguments(bundle1);
        fragmentList.add(informFragment);

    }

    private void addTestData() {
        for (int i = 0; i < 3; i++) {
            ItemLetterBean bean = new ItemLetterBean();
            bean.setHeaderUrlStr("dd");
            bean.setLetterContent("私信私信私信私信私信私信私信私信私信私信私信私信私信私信");
            bean.setNicknameStr("来者报上命来");

            userLetterList.add(bean);
        }

        for (int k = 0; k < 3; k++) {
            ItemLetterBean bean = new ItemLetterBean();
            bean.setHeaderUrlStr("dd");
            bean.setLetterContent("系统消息系统消息系统消息系统消息");
            bean.setNicknameStr("系统嗯哼");

            systemLetterList.add(bean);
        }

        for (int i = 0; i < 10; i++) {
            informList.add("愉快的玩耍愉快的玩耍愉快的玩耍");
        }
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels;
        return width;
    }
}
